<?php

namespace App\Http\Requests;

use App\Http\Requests\Request;

class FriendLinkRequest extends Request
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        $editRules = [
            'id'            => 'required|numeric',
            'name'          => 'required',
            'icon'          => 'sometimes',
            'url'           => 'required',
            'category_code' => 'required|in:1,2',
            'status'        => 'required|boolean',
        ];

        $createRules = [
            'name'          => 'required',
            'icon'          => 'sometimes',
            'url'           => 'required',
            'category_code' => 'required|in:1,2',
            'status'        => 'required|boolean',
        ];

//        return array();

        $data = Request::all();
        //如果是编辑
        if ($data['formType'] == 'edit') {
            return $editRules;
        }
        //如果是创建
        if ($data['formType'] == 'create') {
            return $createRules;
        }
    }

    public function attributes()
    {
        $attributes = array(
            'name'          => '链接名称',
            'icon'          => 'logo图片',
            'url'           => 'url地址',
            'category_code' => '链接类型',
            'status'        => '是否启用',
        );

        return $attributes;
    }

    public function messages()
    {
        $messages = [
            'required'         => ':attribute为必填选项',
            'numeric'          => ':attribute必须为数字',
            'category_code.in' => ':attribute必须为友情链接、合作伙伴中的一种',
            'status.boolean'   => ':attribute必须为启用、暂不启用中的一种',
        ];

        return $messages;
    }
}
